package ie.dkit.semanticPhotoWeb.client;

import java.util.ArrayList;
import java.util.List;

import ie.dkit.semanticPhotoWeb.client.panels.MainPanel;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;
import ie.dkit.semanticPhotoWeb.client.tools.LoginListener;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.layout.FitLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class Main implements EntryPoint {

	private ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private Panel m_WholePage;

	private MainPanel m_MainPanel;

	private List m_LoginListener = new ArrayList();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		String parameters;
		if (!(parameters = getParamString()).equals("")) {
			handleAuthSubToken(parameters.replaceFirst("\\?token=", ""));
		}
		new Viewport(getWholePage());
	}

	public ClientServerConnectorAsync getClientServerConnectorAsync() {
		if (m_ClientServerConnectorAsync == null) {
			m_ClientServerConnectorAsync = ClientServerConnector.Util
					.getInstance();
		}
		return m_ClientServerConnectorAsync;
	}

	private Panel getWholePage() {
		if (m_WholePage == null) {
			m_WholePage = new Panel();
			m_WholePage.setLayout(new FitLayout());
			m_WholePage.add(getMainPanel());
		}
		return m_WholePage;
	}

	private MainPanel getMainPanel() {
		if (m_MainPanel == null) {
			m_MainPanel = new MainPanel(this);
			History.addHistoryListener(m_MainPanel);
			m_LoginListener.add(m_MainPanel.getCenterPanel());
			m_LoginListener.add(m_MainPanel.getStatusPanel());
			m_LoginListener.add(m_MainPanel.getMainToolbar());
		}
		return m_MainPanel;
	}

	private void handleAuthSubToken(String authSubToken) {
		MessageBox.wait("Please wait... Login SemanticPhotoWeb");
		getClientServerConnectorAsync().login(authSubToken,
				new AsyncCallbackAdapter() {
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error. Wasn't able to Login", caught
								.getMessage());
					}

					public void onSuccess(Object result) {
						MessageBox.hide();
						fireLogin((String) result);
					}
				});
	}

	/**
	 * Goto the Google Login Page this will destroy the current session
	 */
	public void gotoLogin() {
		MessageBox.wait("Redirecting to Login...");
		m_ClientServerConnectorAsync.getAuthLink(new AsyncCallbackAdapter() {

			public void onFailure(Throwable caught) {
				MessageBox
						.alert("Wasn't able to contact server please try again later");
			}

			public void onSuccess(Object result) {
				redirect((String) result);
			}

		});
	}

	/**
	 * Logout the System
	 *
	 */
	public void logout() {
		m_ClientServerConnectorAsync.logout(new AsyncCallbackAdapter() {

			public void onSuccess(Object result) {
				fireLogout();
			}

		});
	}

	/**
	 * Simlpy redirects to  the picasaweb site
	 *
	 */
	public void register() {
		redirect("http://picasaweb.google.com/");
	}

	private void fireLogin(String username) {
		for (int i = 0; i < m_LoginListener.size(); i++) {
			((LoginListener) m_LoginListener.get(i)).loggedIn(username);
		}
	}

	private void fireLogout() {
		for (int i = 0; i < m_LoginListener.size(); i++) {
			((LoginListener) m_LoginListener.get(i)).loggedOut();
		}
	}

	/*
	 * 
	 * @param url
	 */
	private static native void redirect(String url) /*-{
	 $wnd.location.href = url;
	 }-*/;

	/*
	 * This method allows me to access the parameters called by the Google login
	 * and transmitt the Authsub
	 */
	private native String getParamString() /*-{
	 return $wnd.location.search;
	 }-*/;
}
